import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import {
  Row,
  Col,
  Card,
  Form,
  Input,
  Select,
  Icon,
  Button,
  Modal,
  Divider, message,
} from 'antd';
import StandardTable from '@/components/StandardTable';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import DescView from './descView';
import styles from '../UserManage.less';
import * as options from '../../../common/options';
import { powerBtnFunc } from '@/utils/utils';

const Option = Select.Option;
const FormItem = Form.Item;
const confirm = Modal.confirm;

/* eslint react/no-multi-comp:0 */
@connect(({ Loan, loading }) => ({
  Loan,
  loading: loading.models.Loan,
}))

@Form.create()
class Loan extends PureComponent {
  state = {
    modalVisible: false,
    updateModalVisible: false,
    lookDescView: false,
    selectedRows: {},
    formValues: {},
    pagination: {
      pageNo: 1,
      pageSize: 15,
    },
  };

  columns = [
    {
      title: '批贷编号',
      key: 'loanNo',
      dataIndex: 'loanNo',
    },
    {
      title: '标的名称',
      dataIndex: 'projectName',
      key: 'projectName',
    },
    {
      title: '企业名称',
      dataIndex: 'enterpriseName',
      key: 'enterpriseName',
    },
    {
      title: '融资金额',
      key: 'amount',
      dataIndex: 'amount',
    },
    {
      title: '已募金额',
      key: 'raiseAmount',
      dataIndex: 'raiseAmount',
    },
    {
      title: '已放款金额',
      key: 'loanAmount',
      dataIndex: 'loanAmount',
    },
    {
      title: '放款状态',
      dataIndex: 'status',
      render: val => <span>{options.loanStatus.map(item => (item.key === val ? item.value : ''))}</span>,
    },
    {
      title: '操作',
      render: (text, record) => (
        <Fragment>
          {
            powerBtnFunc('100096',
              [<a key={'btn'} disabled={record.status !== 1}
                  onClick={() => this.handleUpdateModalVisible(true, record)}>放款确认</a>,
                <Divider key={'Divider'} type="vertical"/>])
          }
          {
            powerBtnFunc('100095',
              <a key={'view'} onClick={() => this.handleLookDesc(true, record)}>查看</a>,
            )}
        </Fragment>
      ),
    },
  ];

  constructor(props) {
    super(props);
  }

  componentDidMount() {
    this.getList();
  }

  getList = () => {
    const { dispatch } = this.props;
    const { pagination } = this.state;
    dispatch({
      type: 'Loan/fetch',
      payload: pagination,
    });
  };
  handleStandardTableChange = (pagination, filtersArg, sorter) => {
    const { dispatch } = this.props;
    const { formValues } = this.state;

    this.setState({
      pagination: {
        pageNo: pagination.current,
        pageSize: 15,
      },
    });
    const params = {
      pageNo: pagination.current,
      pageSize: 15,
      ...formValues,
    };

    dispatch({
      type: 'Loan/fetch',
      payload: params,
    });
  };
  //重置
  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    this.setState({
      formValues: {},
      pagination: {
        pageNo: 1,
        pageSize: 15,
      },
    });
    dispatch({
      type: 'Loan/fetch',
      payload: {},
    });
  };


  handleSelectRows = rows => {
    this.setState({
      selectedRows: rows,
    });
  };
  //搜索
  handleSearch = e => {
    e.preventDefault();

    const { dispatch, form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;

      const values = {
        ...fieldsValue,
      };
      this.setState({
        formValues: values,
      });

      dispatch({
        type: 'Loan/fetch',
        payload: values,
      });
    });
  };
  //修改
  handleUpdateModalVisible = (flag, record) => {
    const { dispatch } = this.props;
    const _this = this;
    confirm({
      title: '请确认?',
      content: '请再次确认是否放款！！',
      onOk() {
        dispatch({
          type: 'Loan/confirm',
          payload: { code: record.loanNo },
          callback(res) {
            if (res.code === 200) {
              _this.getList();
            }
          },
        });
      },
      onCancel() {
      },
    });

  };
  //查看
  handleLookDesc = (flag, record) => {
    const { dispatch } = this.props;
    this.setState({
      lookDescView: !!flag,
      selectedRows: record,
    }, () => {
      if (!!flag) {
        dispatch({
          type: 'Loan/detail',
          payload: { code: record.loanNo },
        });
      }
    });

  };


  renderSimpleForm() {
    const {
      form: { getFieldDecorator },
    } = this.props;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={6} sm={24}>
            <FormItem label="放款编号">
              {getFieldDecorator('loanNo')(<Input placeholder="请输入放款编号"/>)}
            </FormItem>
          </Col>
          <Col md={6} sm={24}>
            <FormItem label="标的名称">
              {getFieldDecorator('projectName')(<Input placeholder="请输入标的名称"/>)}
            </FormItem>
          </Col>
          <Col md={6} sm={24}>
            <FormItem label="放款状态">
              {getFieldDecorator('status')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  {options.loanStatus.map((item) => {
                    return <Option value={item.key} key={item.key}>{item.value}</Option>;
                  })}
                </Select>,
              )}
            </FormItem>
          </Col>
          <Col md={6} sm={24}>
        <span className={styles.submitButtons}>
        <Button type="primary" htmlType="submit">
        查询
        </Button>
        <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
        重置
        </Button>

        </span>
          </Col>
        </Row>
      </Form>
    );
  }


  render() {
    const { Loan: { data, details }, loading } = this.props;
    const { selectedRows, lookDescView } = this.state;
    return (
      <PageHeaderWrapper title="放款管理">
        <Card bordered={false}>

          <div className={styles.tableList}>
            <div className={styles.tableListForm}>{this.renderSimpleForm()}</div>
            <StandardTable
              selectedRows={selectedRows}
              loading={loading}
              data={data ? data : []}
              rowKey='loanNo'
              rowSelectHide={true}
              columns={this.columns}
              onSelectRow={this.handleSelectRows}
              onChange={this.handleStandardTableChange}
            />
          </div>
        </Card>
        <DescView
          handleLookDesc={this.handleLookDesc}
          selectedRows={details ? details : {}}
          lookDescView={lookDescView}/>

      </PageHeaderWrapper>
    );
  }
}

export default Loan;
